/************************************************************************************/
/*  Program name CAPFAIMP_MASS_RECLASS_ITF.sql                                  	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 19 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_MASS_RECLASS_ITF';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_MASS_RECLASS_ITF;

BEGIN
	UPDATE
		FA_MASS_RECLASS_ITF
	SET	
		ASSET_NUMBER	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(ASSET_NUMBER,1,4), ASSET_NUMBER),
		BOOK			=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK,1,4), BOOK)
	WHERE ASSET_NUMBER IN
		(
		SELECT 
			ASSET_NUMBER
		FROM 
			CRST_FA_MASS_RECLASS_ITF
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN		
			INSERT INTO FA_MASS_RECLASS_ITF
			VALUES
			(
			C1.REQUEST_ID,
			CAPBRIMP_MAPPING.GetMassReclassIDMapping(C1.MASS_RECLASS_ID),
			CAPBRIMP_MAPPING.GetAssetIDMapping(C1.ASSET_ID),
			C1.ASSET_NUMBER,
			C1.DESCRIPTION,
			C1.BOOK,
			C1.OLD_CATEGORY,
			C1.NEW_CATEGORY,
			C1.CONVENTION,
			C1.CEILING,
			C1.METHOD,
			C1.LIFE,
			C1.BASIC_RATE,
			C1.ADJUSTED_RATE,
			C1.BONUS_RULE,
			C1.PRODUCTION_CAPACITY,
			C1.UNIT_OF_MEASURE,
			C1.DEPRECIATE_FLAG,
			C1.DEPRN_LIMIT_PERCENTAGE,
			C1.DEPRN_LIMIT_AMOUNT,
			C1.SALVAGE_VAL_PERCENTAGE,
			C1.COST_ACCT,
			C1.DEPRN_RESERVE_ACCT,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/